package com.zdsoft.blp.zongbuju.shouye.linliquan.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.myFragment.CustomFragment;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLXunWuActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.LLZhaoPinActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.activity.XWXQXWActivity;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.SheQuLunTanAdapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.tencent.open.utils.Global.getSharedPreferences;

/**
 * Created by Administrator on 2016/9/2.
 */
public class XWfragmentLeft extends CustomFragment implements NetCallback,XListView.IXListViewListener{
    private XListView xlv;
    private List<LinLi> list;
    private List<LinLi> mlist;
    private String time,name,title,content,phoneNumber,pinglun,fbName,id,imagecontent,touxiang;
    private int pageNo=1,source;
    private SheQuLunTanAdapter adapter;
    @Override
    protected void init() {
        initLayout(R.layout.linli_fragment_es);
        xlv=find(R.id.xlv_ll_es);
        xlv.setXListViewListener(this);
        xlv.setPullRefreshEnable(true);//设置可以刷新
        xlv.setPullLoadEnable(true);//设置可以加载更多
        LLXunWuActivity ac= (LLXunWuActivity) getActivity();
        source=ac.source;
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), XWXQXWActivity.class);
                intent.putExtra("id",list.get(position-1).getId());
                intent.putExtra("title","寻物详情");
                startActivity(intent);
            }
        });
        getNet();
    }


    public void getNet() {
        Net net=new Net();
        net.url=API.INFOEXCHANGE_URL;//infoExchangeService
        net.nameSpace=API.INFOEXCHANGE_NAMESPACE;
        Map map=new HashMap();
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId","");
        String communityCode = sp.getString("communityCode","00001");
        if (source==2){
            net.methodName=API.MYFOUNDLOSTLIST;
            map.put("pageNo",pageNo);
            map.put("pageSize",10);
            map.put("communityCode",communityCode);
            map.put("infoType","xw");
            map.put("condition","");
            map.put("curTime","");
            map.put("userId",userId);
            net.code=2;
            net.parms=map;
        }else {
            net.methodName=API.FOUNDLOSTLIST;
            map.put("pageNo",pageNo);
            map.put("pageSize",10);
            map.put("communityCode",communityCode);
            map.put("infoType","xw");
            map.put("condition","");
            map.put("curTime","");
            net.code=1;
            net.parms=map;
        }

        ((LLXunWuActivity) getActivity()).getServer(net,this);
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag){
            if (pageNo==1){
                mlist=jiexi(code,res);
                adapter = new SheQuLunTanAdapter(mlist, getActivity());
                xlv.setAdapter(adapter);
                xlv.stopRefresh();
            }else {
                list=jiexi(code,res);
                for (LinLi linLis:list){
                    mlist.add(linLis);
                }
                adapter.setListData(mlist);
                xlv.stopLoadMore();
            }
        }
    }

    private List<LinLi> jiexi(int code, String res) {
        try {
            JSONObject object=new JSONObject(res);
            boolean rows=object.isNull("rows");
            if (rows==false){
                list=new ArrayList<>();
                LinLi xunwu=null;
                JSONArray array=object.getJSONArray("rows");
                for (int i=0;i<array.length();i++){
                    JSONObject obj=array.getJSONObject(i);
                    time=obj.getString("createTime");
                    phoneNumber=obj.getString("contactPhone");
                    fbName=obj.getString("contact");
                    pinglun=obj.getString("replyNum");
                    id=obj.getString("id");
                    content=obj.getString("content");
                    imagecontent=obj.getString("picPath");
                    title=obj.getString("title");
                    name=obj.getString("userName");
                    touxiang=obj.getString("headimgurl");
                    int approvedResult=obj.getInt("approvedResult");
                    if (code==1){
                        xunwu=new LinLi(id,touxiang,name,time,title,content,fbName,phoneNumber,imagecontent,pinglun);
                        xunwu.setCode(4);
                    }
                    if (code==2){
                        xunwu=new LinLi(id,touxiang,name,time,title,content,fbName,phoneNumber,imagecontent,pinglun,approvedResult);
                        xunwu.setCode(42);
                    }

                    list.add(xunwu);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        xlv.setAdapter(new SheQuLunTanAdapter(list,getActivity()));
        return list;
    }

    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        getNet();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        getNet();
    }
}
